package com.finley.flowabledemo.design.state.demo02.state;


import cn.hutool.core.util.StrUtil;
import com.finley.flowabledemo.design.state.demo02.source.DatabaseConnection2;
import com.finley.flowabledemo.design.state.demo02.source.EmailSender;
import com.finley.flowabledemo.design.state.demo02.source.Order;
import com.finley.flowabledemo.design.state.demo02.source.OrderStatus;

/**
 * @description:
 * @author: xianfei.chao
 * @date 2025/6/24 17:20
 * @version: 1.0
 */
public class ProcessingStatueState extends OrderStatusState {


    @Override
    void handle(OrderStatusContext orderStatusContext) {
        Order order = orderStatusContext.getOrder();
        if (StrUtil.equalsIgnoreCase(order.getStatus(), OrderStatus.NEW.getCode())) {
            DatabaseConnection2 conn = orderStatusContext.getConn();
            conn.connect();
            conn.executeUpdate("UPDATE orders SET status='PROCESSING' WHERE id=" + order.getId());
            EmailSender emailSender = orderStatusContext.getEmailSender();
            emailSender.sendOrderConfirmation(order.getCustomerEmail(), "订单确认", "您的订单#" + order.getId() + "已确认");
        } else {
            orderStatusContext.setOrderStatusState(new PaidStatueState());
            orderStatusContext.execHandler();
        }
    }
}
